﻿using System;
using System.ComponentModel;
using NewLife.Xml;
using System.Net.Mail;

namespace XTLCMS
{
    /// <summary>邮件设置</summary>
    [XmlConfigFile("Config/Mail.config", 15000)]
    public class MailConfig : XmlConfig<MailConfig>
    {
        private String _Stmp = "";
        /// <summary>STMP服务器</summary>
        [DisplayName("STMP服务器")]
        [Description("发送邮件的SMTP服务器地址")]
        public String Stmp { get { return _Stmp; } set { _Stmp = value; } }

        private Int32 _Port = 25;
        /// <summary>SMTP端口</summary>
        [DisplayName("SMTP端口")]
        [Description("SMTP服务器的端口")]
        public Int32 Port { get { return _Port; } set { _Port = value; } }

        private String _From = "";
        /// <summary>发件人地址</summary>
        [DisplayName("发件人地址")]
        [Description("发件人地址")]
        public String From { get { return _From; } set { _From = value; } }

        private String _Username = "";
        /// <summary>邮箱账号</summary>
        [DisplayName("邮箱账号")]
        [Description("邮箱账号")]
        public String Username { get { return _Username; } set { _Username = value; } }

        private String _Password = "";
        /// <summary>邮箱密码</summary>
        [DisplayName("邮箱密码")]
        [Description("邮箱密码")]
        public String Password { get { return _Password; } set { _Password = value; } }

        private String _Nickname = "";
        /// <summary>发件人昵称</summary>
        [DisplayName("发件人昵称")]
        [Description("显示发件人的昵称")]
        public String Nickname { get { return _Nickname; } set { _Nickname = value; } }

        #region 构造
        /// <summary>
        /// 实例化
        /// </summary>
        public MailConfig()
        {
            Stmp = "SiteEmailStmp";
            Port = 0;
            From = "SiteEmailFrom";
            Username = "SiteUsername";
            Password = "SiteEmailPassword";
            Nickname = "SiteEmailNickname";
        }

        //static MailConfig() { }
        #endregion
        #region 发送电子邮件
        public static void sendMail(string strto, string subj, string bodys)
        {
            MailConfig mc = MailConfig.Current;
            sendMail(mc.Stmp, mc.Username, mc.Password, mc.Nickname, mc.From, strto, subj, bodys);
        }
        /// <summary>
        /// 发送电子邮件
        /// </summary>
        /// <param name="smtpserver">SMTP服务器</param>
        /// <param name="userName">登录帐号</param>
        /// <param name="pwd">登录密码</param>
        /// <param name="nickName">发件人昵称</param>
        /// <param name="strfrom">发件人</param>
        /// <param name="strto">收件人</param>
        /// <param name="subj">主题</param>
        /// <param name="bodys">内容</param>
        public static void sendMail(string smtpserver, string userName, string pwd, string nickName, string strfrom, string strto, string subj, string bodys)
        {
            SmtpClient _smtpClient = new SmtpClient();
            _smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;//指定电子邮件发送方式
            _smtpClient.Host = smtpserver;//指定SMTP服务器
            _smtpClient.Credentials = new System.Net.NetworkCredential(userName, pwd);//用户名和密码

            //MailMessage _mailMessage = new MailMessage(strfrom, strto);
            MailAddress _from = new MailAddress(strfrom, nickName);
            MailAddress _to = new MailAddress(strto);
            MailMessage _mailMessage = new MailMessage(_from, _to);
            _mailMessage.Subject = subj;//主题
            _mailMessage.Body = bodys;//内容
            _mailMessage.BodyEncoding = System.Text.Encoding.Default;//正文编码
            _mailMessage.IsBodyHtml = true;//设置为HTML格式
            _mailMessage.Priority = MailPriority.Normal;//优先级
            _smtpClient.Send(_mailMessage);
        }
        #endregion
    }
}